package com.ljn.boot.config;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.MimeType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;


/**
 * @version 1.0.0
 * @description: SpringMVC配置
 */
@Configuration
//@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ComponentScan(basePackages =
{ "com.ljn"})
//@EnableConfigurationProperties(VelocityProperties.class)
public class SpringMVCConfig extends WebMvcConfigurerAdapter
{

	/**
	 * 错误页面跳转
	 */
	private static final String MESSAGE_EXCEPTION_VIEW = "/message/exception";

	/**
	 * UTF-8编码
	 */
	private static final String CHAR_SET_UTF_8 = "UTF-8";


	/**
	 * @param configurer
	 * @description: 配置内容适配
	 */
	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
	{
		// 解析viewName不继承请求扩展名
		configurer.favorPathExtension(false);
	}

	/**
	 * @return
	 * @description: springmvc请求映射处理器
	 */
	@Bean
	public RequestMappingHandlerAdapter mappingHandlerAdapter()
	{
		RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
		List<HttpMessageConverter<?>> converters = new ArrayList<>(1);
		converters.add(new MappingJackson2HttpMessageConverter());
		adapter.setMessageConverters(converters);
		return adapter;
	}

	/**
	 * @param converters
	 * @description: 配置消息转换器
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
	{
		StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();

		List<MediaType> stringMediaTypes = new ArrayList<>();

		stringMediaTypes.add(new MediaType("text", "plain", Charset.forName(CHAR_SET_UTF_8)));
		stringMediaTypes.add(new MediaType("text", "html", Charset.forName(CHAR_SET_UTF_8)));

		stringHttpMessageConverter.setSupportedMediaTypes(stringMediaTypes);

		converters.add(stringHttpMessageConverter);

		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

		List<MediaType> jsonMediaTypes = new ArrayList<>();

		jsonMediaTypes.add(new MediaType("application", "json", Charset.forName(CHAR_SET_UTF_8)));
		jsonMediaTypes.add(new MediaType("application", "x-www-form-urlencoded", Charset.forName(CHAR_SET_UTF_8)));

		mappingJackson2HttpMessageConverter.setSupportedMediaTypes(jsonMediaTypes);

		converters.add(mappingJackson2HttpMessageConverter);
	}

	/**
	 * @return
	 * @description: springMVC统一异常处理
	 */
	/*@Bean
	public UserHandlerExceptionResolver userHandlerExceptionResolver()
	{
		UserHandlerExceptionResolver userHandlerExceptionResolver = new UserHandlerExceptionResolver();

		Properties exceptionMappings = new Properties();

		exceptionMappings.put("com.ljn.core.AbstractException", MESSAGE_EXCEPTION_VIEW);
		exceptionMappings.put("java.lang.RuntimeException", MESSAGE_EXCEPTION_VIEW);

		userHandlerExceptionResolver.setExceptionMappings(exceptionMappings);

		return userHandlerExceptionResolver;
	}*/

	/**
	 * @param properties
	 * @return
	 * @description: velocity配置
	 * 				@author： zhaiang
	 * @createTime：2017年5月22日 下午7:14:35
	 */
	/*@Bean
	public VelocityLayoutViewResolver velocityViewResolver(VelocityProperties properties)
	{
		properties.setPrefix("/views");
		properties.setSuffix(".vm");
		properties.setCache(false);
		properties.setContentType(MimeType.valueOf("text/html"));
		properties.setCharset(Charset.forName(CHAR_SET_UTF_8));
		properties.setDateToolAttribute("dateTool");
		properties.setEnabled(true);
		properties.setExposeRequestAttributes(true);
		properties.setExposeSessionAttributes(true);
		properties.setExposeSpringMacroHelpers(true);
		properties.setNumberToolAttribute("numberTool");
		properties.setResourceLoaderPath("classpath:/views/");
		properties.setToolboxConfigLocation("/META-INF/velocity/toolbox.xml");
		Map<String, String> attachProperties = new HashMap<>();

		attachProperties.put("velocimacro.library", "inc/macro.vm");
		attachProperties.put("userdirective", "cn.mysteel.web.velocity.directive.RemoteInclude");

		properties.setProperties(attachProperties);

		VelocityLayoutViewResolver resolver = new VelocityLayoutViewResolver();

		resolver.setViewClass(VelocityLayoutView.class);

		Map<String, Object> attributes = new HashMap<>();

		attributes.put("numberTool", NumberTool.class);
		attributes.put("velocityTool", VelocityTool.class);
		attributes.put("urlTool", UrlTool.class);

		resolver.setAttributesMap(attributes);
		properties.applyToViewResolver(resolver);
		resolver.setLayoutUrl("/views/inc/layout.vm");

		return resolver;
	}*/

	/**
	 * 附件上传配置
	 */
/*	@Bean
	public MultipartConfigElement multipartConfigElement()
	{
		MultipartConfigFactory factory = new MultipartConfigFactory();
		// 单个附件的大小
		factory.setMaxFileSize("2MB");
		// 设置总上传数据总大小
		factory.setMaxRequestSize("30MB");
		return factory.createMultipartConfig();
	}*/

	/**
	 * @param registry
	 * @description: SpringMVC拦截器拦截
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry)
	{

		/*// eac拦截器
		BaseEACAuthenticationInterceptor eacInterceptor = new BaseEACAuthenticationInterceptor();
		// 是否做权限拦截，true代表不拦截，false不拦截
		eacInterceptor.setDebug(false);
//		eacInterceptor.setDebug(true);

		// redis模板直连，不要修改属性名称
		eacInterceptor.setEacRedisTemplate(eacRedisTemplate);
		// 部分url不做权限控制，不做拦截，不会注入登陆信息到session中
		Set<String> excludeURIs = new HashSet<>();
		excludeURIs.add("/basedata/category/category_tree.htm");
		excludeURIs.add("/base/sku/sku_spec_rightlist.vm");
		excludeURIs.add("/base/sku/sku_variety_attributes_rightlist.vm");
		eacInterceptor.setExcludeURIs(excludeURIs);

		// 如果为true，代表如果当前员工检测登陆失败，不会跳转到登陆页面，而是会抛出异常
		eacInterceptor.setThrowLogOutException(false);
		// 如果为true，代表如果当前员工检测没有权限，不会跳转到权限提示页面，而是会抛出异常
		eacInterceptor.setThrowNoPermissionException(false);

		registry.addInterceptor(eacInterceptor);
		registry.addInterceptor(new BaseControllerInterceptor()).addPathPatterns("/**").excludePathPatterns("/error");

		super.addInterceptors(registry);*/
	}

}
